package com.pinyoumei.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.ExChangeCouponAdapter;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.bean.ExchangeFreeListBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.view.dialog.BaseDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-06-26.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-06-26        Administrator             新增Person类
 * 2021-06-26        Administrator             增加sex属性
 * 兑换优惠券
 */
public class ExChangeCouponListActivity extends BaseActivity implements View.OnClickListener {

    private int page = 1;
    private ExChangeCouponAdapter exChangeCouponAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_coupon);

        InitWidget();

        InitData();

    }

    private void InitWidget() {
        recyclerView = findViewById(R.id.recyclerView);
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);

        TextView tv_make_rover_type = findViewById(R.id.tv_make_rover_type);
        tv_make_rover_type.setOnClickListener(this);



//        SmartRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
//        //首页数据下拉刷新监听
//        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                page = 1;
//                InitData();
//                refreshLayout.finishRefresh();
//
//            }
//        });
//        swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                page++;
//                LoadData();
//                refreshLayout.finishLoadMore();
//            }
//        });


        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        exChangeCouponAdapter = new ExChangeCouponAdapter(R.layout.item_free_coupon_list2,null);
        recyclerView.setAdapter(exChangeCouponAdapter);
        exChangeCouponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //取消订单
                BaseDialog orderDialog = new BaseDialog(ExChangeCouponListActivity.this, R.layout.dialog_tip);
                orderDialog.setCancelable(false);
                orderDialog.setCanceledOnTouchOutside(false);
                orderDialog.show();
                Button bt_cancel = orderDialog.findViewById(R.id.bt_cancel);
                bt_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderDialog.dismiss();
                        
                    }
                });

                Button bt_sure = orderDialog.findViewById(R.id.bt_sure);

                bt_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //确定
                        SubmitData(exChangeCouponAdapter.getData().get(position).getId());
                        orderDialog.dismiss();

                    }
                });
            }
        });

    }

    private void LoadData() {

    }

    private void InitData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ExChangeCouponListActivity.this));


        OkGo.<String>post(Constants.ZBK_FREE_EXCHANGE)
                .params("user_id", SPUtils.getAppUserId(ExChangeCouponListActivity.this))
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        ExchangeFreeListBean exchangeFreeListBean = gson.fromJson(response.body(), ExchangeFreeListBean.class);
                        if (exchangeFreeListBean.getCode() >= 0) {
                            exChangeCouponAdapter.setNewData(exchangeFreeListBean.getResult().getExchange_coupon_list());

                            //空页面
                            View notDataView = getLayoutInflater().inflate(R.layout.item_empty_view, (ViewGroup) recyclerView.getParent(), false);
                            exChangeCouponAdapter.setEmptyView(notDataView);
                        } else {
                            ToastUtils.showShort(exchangeFreeListBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }




    private void SubmitData(String exchange_coupon_id) {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ExChangeCouponListActivity.this));
        map.put("exchange_coupon_id", exchange_coupon_id);



        OkGo.<String>post(Constants.ZBK_FREE_EXCHANGE_SUBMIT)
                .params("user_id", SPUtils.getAppUserId(ExChangeCouponListActivity.this))
                .params("exchange_coupon_id", exchange_coupon_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        BaseBean baseBean = gson.fromJson(response.body(), BaseBean.class);
                        if (baseBean.getCode() >= 0) {
                            Toast.makeText(ExChangeCouponListActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            ToastUtils.showShort(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.relative_finish:
                finish();
                break;
            case R.id.tv_make_rover_type:
//                startActivity(new Intent(ExChangeCouponListActivity.this, PointManageActivity.class));
                break;

        	default:
        		break;
        }
    }
}
